<?php


// memcache
require_once "cache/MemCachedClient.inc.php";
require_once "cache_config.php";

$memc = new MemCachedClient($_CACHE['options']);


class SmartCache {
        /**
         * Default Cache Lifetime in Seconds
         * Can be overwritten by global configuration array $_CONFIG['cache_lifetime']
         *
         * @access public
         */
        var $lifetime = 600;

        var $key;
        var $memc;

        /**
         * SmartCache Constructor
         *
         * @access public
         * @param mixed $key Unique Container Key
         * @return void
         */
        function SmartCache ($key = '') {
                global $_CONFIG;
                global $memc;

                $this->memc = $memc;

                if (!empty($_CONFIG['cache_dir'])) {
                        $this->cache_dir = $_CONFIG['cache_dir'];
                }
                if (is_numeric($_CONFIG['cache_lifetime'])) {
                        $this->lifetime = $_CONFIG['cache_lifetime'];
                }
                if (empty($key)) {
                        $key = $_SERVER['REQUEST_URI'];
                }


                $this->key = 'cache_' . md5(serialize($key));
        }

        /**
         * Start Ouput Content Buffering
         *
         * @access public
         * @param int $timeout Previously stored content is only considered valid if not older than [timeout] seconds
         * @return void
         */
        function start($timeout = 0) {
                if (empty($_POST) && $_SERVER['HTTP_CACHE_CONTROL'] != 'no-cache') {
                        if ($output = $this->read($timeout)) {
                                exit ($output);
                        } else {
                                ob_start(array(&$this, 'callback'));
                        }
                }
        }

        /**
         * Check if any reusable content is available for this specific Cache Container and read it
         *
         * @access public
         * @param int $timeout Previously stored content is only considered valid if not older than [timeout] seconds
         * @return mixed
         */
        function read ($timeout = 0) {
                if (is_numeric($timeout)) {
                        $this->lifetime = $timeout;
                }

                /*
                if (@is_file($this->cache_dir . $this->filename)) {
                        if ((time() - filemtime($this->cache_dir . $this->filename)) < $this->lifetime) {
                                if ($hd = @fopen($this->cache_dir . $this->filename, "r")) {
                                        $serialized = fread($hd, filesize($this->cache_dir . $this->filename));
                                        fclose($hd);
                                        return unserialize($serialized);
                                }
                        }
                }
                */
                return $this->memc->get($this->key);
        }

        /**
         * Write Content for later reuse
         *
         * @access public
         * @param mixed $content
         * @return void
         */
        function write ($content) {
         	/*
                if ($hd = @fopen($this->cache_dir . $this->filename, 'w')) {
                        if (flock($hd, LOCK_EX)) { // do an exclusive lock
                                fputs($hd, serialize($content));
                                flock($hd, LOCK_UN); // release the lock
                        }
                        fclose($hd);
                }
                */
                $this->memc->set($this->key, $content, $this->lifetime);
        }

        /**
         * Delete Cache Container to force content recreation on next call
         *
         * @access public
         * @return void
         */
        function remove () {
                /*
         	if (is_file($this->cache_dir . $this->filename)) {
                        unlink($this->cache_dir . $this->filename);
                }
                */
                $this->memc->delete($this->key);
        }

        /**
         * Output Buffer Callback Function
         *
         * @access private
         * @param string $output
         * @return string $output
         */
        function callback ($output) {
                $this->write($output);
                return $output;
        }
}

function disconnect_all_memcache() {
	global $memc;
	$memc->disconnect_all();

	// debug
        /*
	$log = date("Y-m-d H:i:s") . ": disconnect_all_memcache\n";
	$fp = fopen("/var/tmp/memcache.log", 'a');
	fwrite($fp, $log);
	fclose($fp);
        */
}
register_shutdown_function('disconnect_all_memcache');

?>